<template>
  <el-dialog
    :title="!dataForm.number ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="题目内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="题目内容"></el-input>
    </el-form-item>
    <el-form-item label="选项A" prop="a">
      <el-input v-model="dataForm.a" placeholder="选项A"></el-input>
    </el-form-item>
    <el-form-item label="选项B" prop="b">
      <el-input v-model="dataForm.b" placeholder="选项B"></el-input>
    </el-form-item>
    <el-form-item label="选项C" prop="c">
      <el-input v-model="dataForm.c" placeholder="选项C"></el-input>
    </el-form-item>
    <el-form-item label="选项D" prop="d">
      <el-input v-model="dataForm.d" placeholder="选项D"></el-input>
    </el-form-item>
    <el-form-item label="正确答案" prop="correctanswer">
      <el-input v-model="dataForm.correctanswer" placeholder="正确答案"></el-input>
    </el-form-item>
    <el-form-item label="分值" prop="value">
      <el-input v-model="dataForm.value" placeholder="分值"></el-input>
    </el-form-item>
    <el-form-item label="必做题目" prop="ismust">
      <el-select v-model="dataForm.ismust" placeholder="比作题目">
        <el-option key="0" label="否" value="0"></el-option>
        <el-option key="1" label="是" value="1"></el-option>
      </el-select>
<!--      <el-input v-model="dataForm.ismust" placeholder="必做题目（是1否0）"></el-input>-->
    </el-form-item>
    <el-form-item label="废弃题目" prop="isuse">
      <el-select v-model="dataForm.isuse" placeholder="废弃题目">
        <el-option key="0" label="否" value="0"></el-option>
        <el-option key="1" label="废弃" value="1"></el-option>
      </el-select>
<!--      <el-input v-model="dataForm.isuse" placeholder="废弃题目（是1否0）"></el-input>-->
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          number: 0,
          content: '',
          a: '',
          b: '',
          c: '',
          d: '',
          correctanswer: '',
          value: '',
          ismust: '',
          isuse: ''
        },
        dataRule: {
          content: [
            { required: true, message: '题目内容不能为空', trigger: 'blur' }
          ],
          a: [
            { required: true, message: '选项A不能为空', trigger: 'blur' }
          ],
          b: [
            { required: true, message: '选项B不能为空', trigger: 'blur' }
          ],
          correctanswer: [
            { required: true, message: '正确答案不能为空', trigger: 'blur' }
          ],
          value: [
            { required: true, message: '分值不能为空', trigger: 'blur' }
          ],
          ismust: [
            { required: true, message: '必做题目（是1否0）不能为空', trigger: 'blur' }
          ],
          isuse: [
            { required: true, message: '废弃题目（是1否0）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.number = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.number) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/question/info/${this.dataForm.number}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.content = data.question.content
                this.dataForm.a = data.question.a
                this.dataForm.b = data.question.b
                this.dataForm.c = data.question.c
                this.dataForm.d = data.question.d
                this.dataForm.correctanswer = data.question.correctanswer
                this.dataForm.value = data.question.value
                this.dataForm.ismust = data.question.ismust
                this.dataForm.isuse = data.question.isuse
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/question/${!this.dataForm.number ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'number': this.dataForm.number || undefined,
                'content': this.dataForm.content,
                'a': this.dataForm.a,
                'b': this.dataForm.b,
                'c': this.dataForm.c,
                'd': this.dataForm.d,
                'correctanswer': this.dataForm.correctanswer,
                'value': this.dataForm.value,
                'ismust': this.dataForm.ismust,
                'isuse': this.dataForm.isuse
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
